package com.pizza.control;

import java.rmi.RemoteException;
import java.util.Date;
import java.util.Properties;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.pizza.database.PizzaManagerClient;
import com.pizza.model.Client;
import com.sun.faces.util.MessageFactory;

/**
 * Class SendMailBean
 *
 */
public class SendMailBean
{
	public String mail;

	public void sendPasswordByMail()
	{
		try
		{
			FacesContext context = FacesContext.getCurrentInstance();
			FacesMessage message = null;
			Client client = PizzaManagerClient.GetPizzaManager().getClientByMail(mail);
			if (client == null)
			{
				message = MessageFactory.getMessage(context, "input_mail_unknown");
				message.setSeverity(FacesMessage.SEVERITY_ERROR);
				context.addMessage("input_mail", message);
			} else
			{
				String mail_content = "Voilà votre mot de passe: "
						+ client.getMotdepasse();
				send("nepasrepondre@epizza.fr", mail, "", "", "[Mot de passe oublié]",
						mail_content, "etudiant.univ-brest.fr");
			}
		} catch (RemoteException e)
		{
			e.printStackTrace();
		}
	}

	protected void send(String p_from, String p_to, String p_cc, String p_bcc,
			String p_subject, String p_message, String p_smtpServer)
	{
		// Name of the Host machine where the SMTP server is running
		String l_host = p_smtpServer;

		// Gets the System properties
		Properties l_props = System.getProperties();

		// Puts the SMTP server name to properties object
		l_props.put("mail.smtp.host", l_host);

		// Get the default Session using Properties Object
		Session l_session = Session.getDefaultInstance(l_props, null);

		try
		{
			MimeMessage l_msg = new MimeMessage(l_session); // Create a New
			// message

			l_msg.setFrom(new InternetAddress(p_from)); // Set the From address

			// Setting the "To recipients" addresses
			l_msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(p_to,
					false));

			// Setting the "Cc recipients" addresses
			l_msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(p_cc,
					false));

			// Setting the "BCc recipients" addresses

			l_msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(p_bcc,
					false));

			l_msg.setSubject(p_subject); // Sets the Subject

			// Create and fill the first message part
			MimeBodyPart l_mbp = new MimeBodyPart();
			l_mbp.setText(p_message);

			// Create the Multipart and its parts to it
			Multipart l_mp = new MimeMultipart();
			l_mp.addBodyPart(l_mbp);

			// Add the Multipart to the message
			l_msg.setContent(l_mp);

			// Set the Date: header
			l_msg.setSentDate(new Date());

			// Send the message
			Transport.send(l_msg);
			// If here, then message is successfully sent.

		} catch (MessagingException mex)
		{ // Trap the MessagingException Error
			mex.printStackTrace();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public String getMail()
	{
		return mail;
	}

	public void setMail(String mail)
	{
		this.mail = mail;
	}
}
